
package controller.admin;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.validator.ValidatorException;
import javax.persistence.Query;
import model.*;
import model.ApplicantFacade;


@ManagedBean(name = "hrmb")
@SessionScoped
public class RoleHrGroupManagedBean {
    //Interview
    
    @EJB
    private InterviewFacade interviewFacade;
    private Interview in = new Interview();

    public InterviewFacade getInterviewFacade() {
        return interviewFacade;
    }

    public void setInterviewFacade(InterviewFacade interviewFacade) {
        this.interviewFacade = interviewFacade;
    }

    public Interview getIn() {
        return in;
    }

    public void setIn(Interview in) {
        this.in = in;
    }
    @EJB
    private CvFacade cvFacade;
    @EJB
    private EmployeeFacade employeeFacade;
    
    
    private String CvID;
    private String EmployeeID;

    public String getCvID() {
        return CvID;
    }

    public void setCvID(String CvID) {
        this.CvID = CvID;
    }

    public String getEmployeeID() {
        return EmployeeID;
    }

    public void setEmployeeID(String EmployeeID) {
        this.EmployeeID = EmployeeID;
    }
    public List<Interview> interList() {
        return this.interviewFacade.findAll();
    }
    public String addInter() {
            String page ="";
            int li = interList().size();
            for (int i = 0; i < li; i++) {
                if (interList().get(i).getInterviewID().equals(this.in)){
                    page="CreateInter";
                }else
                {
                    this.in = this.in;
                }
            }
            if(this.in.getEndedTime().getDate()-this.in.getStartedTime().getDate() > 0){
                this.in.setEmployeeID(this.employeeFacade.find(this.EmployeeID));
                        this.in.setCvid(this.cvFacade.find(this.CvID));
                        this.interviewFacade.create(in);
                        page ="ListInterview";
            }
            else{
                
                 page="CreateInter";
            }
                         
                
            
        return page;
    }
    public String editInter() {
        String page ="";
            int li = interList().size();
            for (int i = 0; i < li; i++) {
                if (interList().get(i).getInterviewID().equals(this.in)){
                    page="EditInter";
                }else
                {
                    this.in = this.in;
                }
            }
            if(this.in.getEndedTime().getDate()-this.in.getStartedTime().getDate() > 0){
                this.in.setEmployeeID(this.employeeFacade.find(this.EmployeeID));
                        this.in.setCvid(this.cvFacade.find(this.CvID));
                        this.interviewFacade.edit(in);
                        page ="ListInterview";
            }
            else{
                
                 page="EditInter";
            }
                         
                
            
        return page;
        
    }
    public String interEdit(Interview inter) {
        this.in = inter;
        return "EditInter";
    }

    public String interView(Interview inter) {
        this.in = inter;
        return "ViewInter";
    }
    
    
    
    //Applicant
    @EJB
    private ApplicantFacade applicantFacade;
    private Applicant app = new Applicant();
    Date date =new Date();

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public ApplicantFacade getApplicantFacade() {
        return applicantFacade;
    }

    public void setApplicantFacade(ApplicantFacade applicantFacade) {
        this.applicantFacade = applicantFacade;
    }

    public Applicant getApp() {
        return app;
    }

    public void setApp(Applicant app) {
        this.app = app;
    }
    
    

    
    public RoleHrGroupManagedBean() {
    }
    
    public List<Applicant> listapp(){
        return this.applicantFacade.findAll();
    }
    public String addapplicant(){
       String page ="CreateApp";
       try{     
           this.app.setCreatedDate(new Date());
           this.app.setStatus(1);
           this.applicantFacade.create(this.app);
          
           page = "ListApplicant";    
       }
        catch(Exception ex){
            System.out.println(ex.getMessage());
        }
       return page;
       
    }
    public String editApplicant(){
        this.applicantFacade.edit(app);
        return "ListApplicant";
    }
    public String appEdit(Applicant appli){
        this.app = appli;
        return "EditApp";
    }
     public String appView(Applicant appli){
        this.app = appli;
        return "ViewApp";
    }
     
     //Vacancy
     
     @EJB
    private DepartmentFacade departmentFacade;
    
    @EJB
    private VacancyFacade vacancyFacade;
    private Vacancy vaca = new Vacancy();
    
    
    private String DepartmentID;

    public String getDepartmentID() {
        return DepartmentID;
    }

    public void setDepartmentID(String DepartmentID) {
        this.DepartmentID = DepartmentID;
    } 
     public VacancyFacade getVacancyFacade() {
        return vacancyFacade;
    }

    public void setVacancyFacade(VacancyFacade vacancyFacade) {
        this.vacancyFacade = vacancyFacade;
    }

    public Vacancy getVac() {
        return vaca;
    }

    public void setVac(Vacancy vac) {
        this.vaca = vac;
    }
     public String editvaca(){
        this.vacancyFacade.edit(this.vaca);
        return "ListVaca";
    }
    
    public List<Vacancy> vacancyList(){
        return this.vacancyFacade.findAll();
    }
    public String createvaca(){
       
        String vacancyID= generateID();
        try
        {
            this.vaca.setVacancyID(vacancyID);
            this.vaca.setDepartmentID(this.departmentFacade.find(this.DepartmentID));
            this.vaca.setCreatedDate(new Date());
         this.vacancyFacade.create(this.vaca);
         
        }
        catch(Exception ex)
        {
            
        }                
        
        return "ListVaca";
        
    }
    public String vacaEdit(Vacancy va){
        this.vaca = va;
        return "EditVaca";
    }
    public String vacaView(Vacancy va){
        this.vaca = va;
        return "ViewVaca";
    }
    private String generateID() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        
        return "V" + sdf.format(new Date());
    }
    
    //Seach
    
    private String input;

    public String getInput() {
        return input;
    }

    public void setInput(String input) {
        this.input = input;
    }
    private String object;

    public String getObject() {
        return object;
    }

    public void setObject(String object) {
        this.object = object;
    }

    
 
    public List<Applicant> Applicantsearch(){
        Applicant ap= new Applicant();
        String page = "Search";
        List<Applicant> la = new ArrayList<Applicant>();
        //List<Object> ls = new ArrayList<Object>();
        try {
            Query query = (Query) applicantFacade.getEntityManager().createNamedQuery("Applicant.Search");
            query.setParameter("firstName", "%"+input+"%");
            la = (List<Applicant>) query.getResultList();
        } catch (Exception err) {
            FacesMessage message = new FacesMessage();
            message.setDetail("Applicant not found");
            message.setSummary("Applicant not found");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }     
        return la;
    }
    public List<Vacancy> vacancysearch(){
        Vacancy va= new Vacancy();
        String page = "Search";
        List<Vacancy> lv = new ArrayList<Vacancy>();
        try {
            Query query = (Query) vacancyFacade.getEntityManager().createNamedQuery("Vacancy.search");
            query.setParameter("title", "%"+input+"%");
            lv = (List<Vacancy>) query.getResultList();
        } catch (Exception err) {
            FacesMessage message = new FacesMessage();
            message.setDetail("Vacancy not found");
            message.setSummary("vacancy not found");
            message.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message);
        }     
        return lv;
    }
    public String search(){
         String page = "";
        if(this.object.equals("a")){
             page= "ShowSearchApp";
        }
        else if(this.object.equals("vac")){
             page= "ShowSearchVac";
        }
        return page;
    }
    
    
    
    //CV
   private Cv cv = new Cv();
   
   private String appID;
   private String vacaID;

    public String getAppID() {
        return appID;
    }

    public void setAppID(String appID) {
        this.appID = appID;
    }

    public String getVacaID() {
        return vacaID;
    }

    public void setVacaID(String vacaID) {
        this.vacaID = vacaID;
    }

   

    public CvFacade getCvFacade() {
        return cvFacade;
    }

    public void setCvFacade(CvFacade cvFacade) {
        this.cvFacade = cvFacade;
    }

    public Cv getCv() {
        return cv;
    }

    public void setCv(Cv cv) {
        this.cv = cv;
    }
   public String addcv(){
        String CvID =generateID();
       this.cv.setCvid(CvID);
        this.cv.setApplicantID(this.applicantFacade.find(this.appID));
        this.cv.setVacancyID(this.vacancyFacade.find(this.vacaID));
        this.cvFacade.create(this.cv);
        return "ListCv";
    }
    public String editcv(){
        this.cv.setApplicantID(this.applicantFacade.find(this.appID));
        this.cv.setVacancyID(this.vacancyFacade.find(this.vacaID));
        this.cvFacade.edit(this.cv);
        return "ListCv";
    }
    public List<Cv> cvList(){
        return this.cvFacade.findAll();
    }
    public String cvEdit(Cv c){
        this.cv = c;
        return "EditCv";
    }
    public String cvView(Cv c){
        this.cv = c;
        return "ViewCv";
    }
      
     
}
